<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
      <el-breadcrumb-item :to="{ path: '/' }">后台管理</el-breadcrumb-item>
      <el-breadcrumb-item>评论管理</el-breadcrumb-item>
    </el-breadcrumb>



    <el-divider></el-divider>

    <el-form :inline="true" :model="select" class="demo-form-inline">
      <el-form-item label="用户昵称">
        <el-input v-model="select.nickName" placeholder="用户昵称"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button  type="primary" @click="selectByNickName(select.nickName)">查询</el-button>
      </el-form-item>
      <el-form-item label="书名">
        <el-input v-model="select.goodsName" placeholder="书名"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button  type="primary" @click="selectByName(select.goodsName)">查询</el-button>
      </el-form-item>
    </el-form>

    <el-divider></el-divider>

    <el-table
        :data="tableData"
        border
        style="width: 100%">
      <el-table-column prop="id" label="编号" width="50" align="center"></el-table-column>
      <el-table-column prop="nickName" label="用户昵称" width="150" align="center"></el-table-column>
      <el-table-column prop="goodsName" label="商品名称" width="200" align="center"></el-table-column>
      <el-table-column prop="comments" label="评论" width="500" align="center"></el-table-column>
      <el-table-column label="操作" width="50" align="center">
        <template slot-scope="scope">
          <el-button type="danger" icon="el-icon-delete" circle size="mini" @click="openDelectConfirm(scope.row)"></el-button>
        </template>
      </el-table-column>

    </el-table>


  </div>
</template>
<script>
import axios from "axios";

export default {
  data() {
    return {
      tableData: [],
      select: []
    }
  },
  methods:{
    selectByName(goodsName){
      let url='http://localhost:8080/comment/commentsByName/'+goodsName;
      this.axios.get(url).then((response)=>{
        let responseBody=response.data;
        if(responseBody.state==20000){
          this.tableData=responseBody.data;
          this.$message({
            type: 'success',
            message: "查询成功"
          });
        }else {
          this.$message({
            type: 'info',
            message: responseBody.message
          });
        }
      })
    },
    selectByNickName(nickname){
      let url='http://localhost:8080/comment/commentsByNickName/'+nickname;
      this.axios.get(url).then((response)=>{
        let responseBody=response.data;
        if(responseBody.state==20000){
          this.tableData=responseBody.data;
          this.$message({
            type: 'success',
            message: "查询成功"
          });
        }else {
          this.$message({
            type: 'info',
            message: responseBody.message
          });
        }
      })
    },
    handleDelect(comment){
      console.log("准备删除"+comment.id)
      let url='http://localhost:8080/comment/delete/'+comment.id;
      this.axios.post(url).then((response)=>{
        let responseBody=response.data;
        if(responseBody.state==20000){
          this.loadCommentsList();
          this.$message({
            type: 'success',
            message: '删除成功'
          });
          this.loadCommentsList();
        }else {
          this.$message({
            type: 'info',
            message: responseBody.message
          });
          this.loadCommentsList();
        }
      })
    },
    openDelectConfirm(comment){
      let message='此操作将永久删除该评论《'+comment.comments+'》, 是否继续???';
      this.$confirm(message, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelect(comment);
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    loadCommentsList(){
      console.log('commentlist')
      let url='http://localhost:8080/comment';
      console.log('url='+url);
      this.axios.get(url).then((response)=>{
        let responseBody=response.data;
        this.tableData=responseBody.data;
      })
    }
  },
  mounted() {//挂载
    console.log('mounted')
    this.loadCommentsList();
  }

}
</script>